package com.ws.universal.tools.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import androidx.core.content.ContextCompat;

import java.io.File;

/**
 * @author:ws
 * @date: 2021/7/29 14:31
 * @desc 图片转换
 */
public final class BitmapUtils {
    public static Bitmap getBitmapFormResources(Context context, int resId) {
        return BitmapFactory.decodeResource(context.getResources(), resId);
    }


    public static Drawable getDrawableFromResources(Context context, int resId) {
        return ContextCompat.getDrawable(context, resId);
    }


    public static Drawable getDrawbleFormBitmap(Context context, Bitmap bitmap) {
        return new BitmapDrawable(context.getResources(), bitmap);
    }

    /**
     * drawable转换成Bitmap
     *
     * @param drawable
     * @return
     */
    public static Bitmap getBitmapFormDrawable(Drawable drawable) {
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        //设置绘画的边界，此处表示完整绘制
        drawable.draw(canvas);
        return bitmap;
    }

    public static String getPath(Context context, String name) {
        return context.getFilesDir() + File.separator + name;

    }


}
